package fi.jamk.ourcookingapp.backend.architecture.services;

import java.io.InputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;
import fi.jamk.ourcookingapp.backend.architecture.json.WebService;

/**
 * Setting the image for the receipt in the different views
 * Taken from 
 * http://stackoverflow.com/questions/2471935/how-to-load-an-imageview-by-url-in-android
 */
public class DownloadImageService extends AsyncTask<String, Void, Bitmap> {
	
	private ImageView bmImage;
	
	public DownloadImageService(ImageView receiptImage) {
		this.bmImage = receiptImage;
	}

	@Override
	protected Bitmap doInBackground(String... urls) {
		String urldisplay = WebService.DATASERVICE_URL + "/" + urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mIcon11;
	}
	
	protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }

}
